Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@csstools/postcss-random-function

Package Overview
Dependencies
Maintainers
3
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@csstools/postcss-random-function

Use the random function in CSS

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
3
Created
Source

PostCSS Random Function PostCSS Logo

npm install @csstools/postcss-random-function --save-dev

PostCSS Random Function lets you use the random function, following the CSS Values 5 specification.

div {
	color: oklch(0.7, 0.2, random(120deg, 240deg));
}

div {
	color: oklch(0.7, 0.2, random(120deg, 240deg, by 7deg));
}

div {
	color: oklch(0.7, 0.2, random(--text, 120deg, 240deg));
	border-color: oklch(0.7, 0.2, random(--border, 120deg, 240deg));
}

/* becomes */

div {
	color: oklch(0.7, 0.2, 238.66036deg);
}

div {
	color: oklch(0.7, 0.2, 134deg);
}

div {
	color: oklch(0.7, 0.2, 226.47057deg);
	border-color: oklch(0.7, 0.2, 157.76966deg);
}

[!NOTE] Generated values are deterministic pseudo random numbers. Generating values twice with the same input will give the same result. The input length of the CSS source file is used as a random seed.

Usage

Add PostCSS Random Function to your project:

npm install postcss @csstools/postcss-random-function --save-dev

Use it as a PostCSS plugin:

const postcss = require('postcss');
const postcssRandomFunction = require('@csstools/postcss-random-function');

postcss([
	postcssRandomFunction(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);

⚠️ About custom properties

Given the dynamic nature of custom properties it's impossible to know what the variable value is, which means the plugin can't compute a final value for the stylesheet.

Because of that, any usage that contains a var is skipped.

Options

preserve

The preserve option determines whether the original notation is preserved. By default, it is not preserved.

postcssRandomFunction({ preserve: true })
div {
	color: oklch(0.7, 0.2, random(120deg, 240deg));
}

div {
	color: oklch(0.7, 0.2, random(120deg, 240deg, by 7deg));
}

div {
	color: oklch(0.7, 0.2, random(--text, 120deg, 240deg));
	border-color: oklch(0.7, 0.2, random(--border, 120deg, 240deg));
}

/* becomes */

div {
	color: oklch(0.7, 0.2, 238.66036deg);
	color: oklch(0.7, 0.2, random(120deg, 240deg));
}

div {
	color: oklch(0.7, 0.2, 134deg);
	color: oklch(0.7, 0.2, random(120deg, 240deg, by 7deg));
}

div {
	color: oklch(0.7, 0.2, 226.47057deg);
	color: oklch(0.7, 0.2, random(--text, 120deg, 240deg));
	border-color: oklch(0.7, 0.2, 157.76966deg);
	border-color: oklch(0.7, 0.2, random(--border, 120deg, 240deg));
}

Keywords

FAQs

Package last updated on 11 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc